home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group93b.txt / 000065_icon-group-sender _Fri May 7 08:15:04 1993.msg < prev    next >
Internet Message Format  |  1993-06-16  |  2KB

  1. Received: by cheltenham.cs.arizona.edu; Fri, 7 May 1993 11:40:44 MST
  2. Message-Id: <9305071512.AA14957@enlil.premenos.sf.ca.us>
  3. From: Ken Walker <kwalker@shara.premenos.sf.ca.us>
  4. Subject: Re: Is this passable code?
  5. To: icon-group@cs.arizona.edu
  6. Date: Fri, 7 May 93 8:15:04 PDT
  7. In-Reply-To: <1993Apr30.130203.1@skyler.mavd.honeywell.com>; from "sol.ctr.columbiaicon-group@cs.arizona.edu" at Apr 30, 93 7:02 pm
  8. Mailer: Elm [revision: 66.25]
  9. Status: R
  10. Errors-To: icon-group-errors@cs.arizona.edu
  11.  
  12. > Don J. Bailey writes:
  13. > In article <9304260508.AA21290@internal.apple.com>, nevin@apple.com (Nevin ":-]" Liber) writes:
  14. > > 
  15. > > procedure Permute(sString)
  16. >    ...
  17. > Ok, I understand it's a generator. I understand it's recursive. 
  18. > I understand it works because I have run it and traced it. I don't 
  19. > understand why it works or how you thought of the design.
  20.  
  21. Here is a permutation procedure that produces a set of permutations. It
  22. is not as neat as Nevin's recursive generator, but is perhaps a little
  23. easier to follow. Once you understand it, it may be easier to
  24. understand Nevin's code; just think of a generator as returning
  25. elements of a set, one at a time.
  26.  
  27. # permutations - return the set of all permuation of the characters in s
  28. procedure permutations(s)
  29.    local p, pi, i
  30.  
  31.    if *s <= 1 then
  32.       return set([s])
  33.  
  34.    # Choose each character in turn and concatenate it on the front of each
  35.    # of the permutations of the remaining characters.
  36.    p := set()
  37.    every i := 1 to *s do {
  38.      pi := permutations(s[1:i] || s[i+1:0])
  39.      every insert(p, s[i] || !pi)
  40.      }
  41.    return p
  42. end
  43.  
  44.  
  45. Ken Walker   kwalker@premenos.sf.cs.us
  46.